<?php namespace Phpcmf\Model\Tixian;

class Tixian extends \Phpcmf\Model
{

    // 提现手续费比例
    public function cash_price($member) {

        if (!$member) {
            return 0;
        }

        $price = [];

	    $config = \Phpcmf\Service::M('app')->get_config('tixian');

        if ($config['group_price'] && is_array($config['group_price'])) {
            foreach ($config['group_price'] as $gid => $value) {
                if (in_array($gid, $member['groupid'])) {
                    $price[] = $value;
                }
            }
            if ($price) {
                return intval(min($price));
            }
        }

        return 0;
    }

    // 最小提现值
    public function min_price() {
	    $config = \Phpcmf\Service::M('app')->get_config('tixian');
        return $config['min'];
    }

    /// 提现申请
    public function add_cash($post) {

        if (!$this->uid || !$post) {
            return dr_return_data(0, dr_lang('参数错误'));
        } elseif (strlen($post['value']) > 8) {
            return dr_return_data(0, dr_lang('金额不规范'));
        }

        // 手续费
        $price = $this->cash_price($this->member);

        $rt = $this->table('member_cashlog')->insert([
            'uid' => $this->uid,
            'username' => $this->member['username'],
            'content' => dr_safe_replace($post['content']),
            'value' => $post['value'],
            'money' => $price ? $post['value'] - $post['value'] * ($price/100) : $post['value'],
            'status' => 0,
            'result' => '',
            'paytime' => 0,
            'inputtime' => SYS_TIME,
        ]);
        if (!$rt['code']) {
            return $rt;
        }

        // 扣钱
        $rt2 = \Phpcmf\Service::M('member')->add_freeze($this->uid, $post['value']);
        if (!$rt2['code']) {
            // 删除刚刚添加的数据
            $this->table('member_cashlog')->delete($rt['code']);
        }

        return $rt;
    }

    // 提现申请处理拒绝
    public function cash_fail($id, $post, $old) {

        $this->table('member_cashlog')->update($id, [
            'result' => $post['result'],
            'status' => 1, // 拒绝
        ]);
        // 取消冻结资金
        return \Phpcmf\Service::M('member')->cancel_freeze($old['uid'], $old['value']);
    }


    // 提现申请处理成功
    public function cash_successs($id, $post, $old) {

        // 增加消费量 并移除冻结资金
        $rt = \Phpcmf\Service::M('member')->use_freeze($old['uid'], $old['value']);
        if (!$rt['code']) {
            return $rt;
        }

        // 标识申请成功
        $this->table('member_cashlog')->update($id, [
            'result' => $post['result'],
            'status' => 2, // 成功
            'paytime' => SYS_TIME,
        ]);

        // 增加消费记录
        return \Phpcmf\Service::M('pay')->add_paylog([
            'uid' => $old['uid'],
            'username' => $old['username'],
            'touid' => $old['uid'],
            'tousername' => $old['username'],
            'mid' => 'cash',
            'title' => dr_lang('现金提现'),
            'value' => -$old['value'],
            'type' => 'finecms',
            'status' => 1,
            'url' => '',
            'result' => $post['result'],
            'paytime' => SYS_TIME,
            'inputtime' => SYS_TIME,
        ]);
    }




}